package com.hjxr.sysadmin.zsj.service.impl;

import java.util.*;

import com.hjxr.common.constant.UserConstants;
import com.hjxr.common.utils.StringUtils;
import com.hjxr.sysadmin.zsj.domain.Inchainlocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjxr.sysadmin.zsj.mapper.IndustrialchainMapper;
import com.hjxr.sysadmin.zsj.domain.Industrialchain;
import com.hjxr.sysadmin.zsj.service.IIndustrialchainService;

/**
 * 产业链信息Service业务层处理
 *
 * @author zc
 * @date 2021-04-15
 */
@Service
public class IndustrialchainServiceImpl implements IIndustrialchainService
{
    @Autowired
    private IndustrialchainMapper industrialchainMapper;

    /**
     * 查询产业链信息
     *
     * @param id 产业链信息ID
     * @return 产业链信息
     */
    @Override
    public Industrialchain selectIndustrialchainById(Long id)
    {
        return industrialchainMapper.selectIndustrialchainById(id);
    }

    /**
     * 查询产业链信息列表
     *
     * @param industrialchain 产业链信息
     * @return 产业链信息
     */
    @Override
    public List<Industrialchain> selectIndustrialchainList(Industrialchain industrialchain)
    {
        return industrialchainMapper.selectIndustrialchainList(industrialchain);
    }

    /**
     * 新增产业链信息
     *
     * @param industrialchain 产业链信息
     * @return 结果
     */
    @Override
    public int insertIndustrialchain(Industrialchain industrialchain)
    {
        if (industrialchain.getParentId() == 0) {
            industrialchain.setInchainName(industrialchain.getInchainSelect());
        } else {
            String regex = "(.{1})";
            String inchainName = "";
            inchainName = industrialchain.getInchainSelect().replaceAll(regex,"$1\\\\n");
            industrialchain.setInchainName(inchainName.substring(0,inchainName.length() - 2));
        }
        industrialchain.setModifiedTime(new Date());
        return industrialchainMapper.insertIndustrialchain(industrialchain);
    }

    /**
     * 修改产业链信息
     *
     * @param industrialchain 产业链信息
     * @return 结果
     */
    @Override
    public int updateIndustrialchain(Industrialchain industrialchain)
    {
        if (industrialchain.getParentId() == 0) {
            industrialchain.setInchainName(industrialchain.getInchainSelect());
        } else {
            String regex = "(.{1})";
            String inchainName = "";
            inchainName = industrialchain.getInchainSelect().replaceAll(regex,"$1\\\\n");
            industrialchain.setInchainName(inchainName.substring(0,inchainName.length() - 2));
        }
        industrialchain.setModifiedTime(new Date());
        return industrialchainMapper.updateIndustrialchain(industrialchain);
    }

    /**
     * 批量删除产业链信息
     *
     * @param ids 需要删除的产业链信息ID
     * @return 结果
     */
    @Override
    public int deleteIndustrialchainByIds(Long[] ids)
    {
        return industrialchainMapper.deleteIndustrialchainByIds(ids);
    }

    /**
     * 删除产业链信息信息
     *
     * @param id 产业链信息ID
     * @return 结果
     */
    @Override
    public int deleteIndustrialchainById(Long id)
    {
        return industrialchainMapper.deleteIndustrialchainById(id);
    }

    /**
     * 校验产业链名称是否唯一
     *
     * @param industrialchain 产业链信息
     * @return 结果
     */
    @Override
    public String checkInchainNameUnique(Industrialchain industrialchain)
    {
        Long id = StringUtils.isNull(industrialchain.getId()) ? -1L : industrialchain.getId();
        Industrialchain info = industrialchainMapper.checkInchainNameUnique(industrialchain.getInchainSelect(), industrialchain.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 是否存在产业链子节点
     *
     * @param id 产业链ID
     * @return 结果
     */
    @Override
    public boolean hasChildByInchainId(Long id)
    {
        int result = industrialchainMapper.hasChildByInchainId(id);
        return result > 0 ? true : false;
    }

    /**
     * 查询产业链主要区域信息
     * @param inchainid
     * @return
     */
    @Override
    public Inchainlocation selectIndustryById(Long inchainid) {
        Inchainlocation inchainlocation = industrialchainMapper.selectIndustryById(inchainid);
        return inchainlocation;
    }

    /**
     * 新增产业链主要区域信息
     * @param inchainlocation
     * @return
     */
    @Override
    public int insertIndustry(Inchainlocation inchainlocation) {
        inchainlocation.setModifiedTime(new Date());
        return industrialchainMapper.insertIndustry(inchainlocation);
    }

    /**
     * 修改产业链主要区域信息
     * @param inchainlocation
     * @return
     */
    @Override
    public int updateIndustry(Inchainlocation inchainlocation) {
        inchainlocation.setModifiedTime(new Date());
        return industrialchainMapper.updateIndustry(inchainlocation);
    }
}
